<?php


namespace App\Http\Services\Admin;


use App\Http\Constants\ApplicationStatusConstants;
use App\Models\CoinsType;
use YiluTech\ComplexSearch\ComplexSearch;

class CoinsTypeService extends ComplexSearch
{
    public $root = CoinsType::class;
    protected $display = 'simple';
    protected $orderBy = 'id desc';

    protected $headers = [
        [
            ['value' => 'id', 'label' => 'ID'],
            ['value' => 'type_code', 'label' => '红包编码'],
            ['value' => 'type_name', 'label' => '业务类型'],
            ['value' => 'created_at', 'label' => '创建时间'],
            ['value' => 'status', 'label' => '状态'],
        ]
    ];

    protected $conditions = [
        'type_code' => [
            'ctype' => 'input',
            'operator' => 'like',
            'label' => '红包编码'
        ],
        'type_name' => [
            'ctype' => 'input',
            'operator' => 'like',
            'label' => '业务类型'
        ],
        'status' => [
            'ctype' => 'select',
            'operator' => ApplicationStatusConstants::STATUS,
            'label' => '状态'
        ],
        'created_at' => [
            'ctype' => 'date-in',
            'label' => '创建时间',
            'format' => 'Y-m-d',
            'display' => true
        ],

    ];

    public function query()
    {
        $select = ['id', 'created_at', 'status', 'type_code', 'type_name', 'subsidy', 'type'];
        $query = parent::query();
        $query->select($select);
        return $query;
    }


    /**
     * Description：详情
     * @param $id
     * @return mixed
     */
    public function detail($id)
    {
        return CoinsType::find($id);

    }

    /**
     * Description：创建
     * @param $data
     * @return mixed
     */
    public function create($data)
    {
        return CoinsType::create($data);

    }

    /**
     * Description：打开
     * @param $id
     * @return mixed
     */
    public function onoff($data)
    {
        $CoinsType = CoinsType::find($data['id']);
        $CoinsType->status = $data['status'];
#TODO：日志添加
        $CoinsType->save();
        return $CoinsType;
    }

    /**
     * Description：关闭
     * @param $id
     * @return mixed
     */
    public function close($id)
    {
        $CoinsType = CoinsType::find($id);
        $CoinsType->status = ApplicationStatusConstants::STOP;
#TODO：日志添加
        $CoinsType->save();
        return $CoinsType;
    }

    public function junior_create($data)
    {
        $parentCoinsType = CoinsType::find($data['id']);
        $data['parent_code'] = $parentCoinsType->type_code;

        $type = $data['type'];
        $type_code = ($type == 1) ? 'A' : 'B';
        $whereMap = [
            ['type', $type],
            ['parent_code', $data['parent_code']],
        ];
        $junior_type_total = CoinsType::where($whereMap)
            ->count('id');
        $junior_type_total++;

        $data['type_code'] = $data['parent_code'] . $type_code . $junior_type_total;
        unset($parentCoinsType, $data['id']);
#TODO：日志添加
        return CoinsType::create($data);
    }

    public function junior_update($data)
    {
        $CoinsApplication = CoinsType::find($data['id']);
        $data_result['subsidy'] = $data['subsidy'];
        $data_result['type_name'] = $data['type_name'];
#TODO：日志添加
        $CoinsApplication->fill($data_result);
        $CoinsApplication->save();
        return $CoinsApplication;
    }

}
